﻿using System;
using System.Management;

namespace Framework
{
    public  class ComputerExtension
    {
        #region Nested type: Computer
        /// <summary>   
        /// Computer Information   
        /// </summary>   

            private static ComputerExtension _instance;
            public string ComputerName;
            public string CpuId;
            public string DiskId;
            public string IpAddress;
            public string LoginUserName;
            public string MacAddress;
            public string SystemType;
            public string TotalPhysicalMemory; //单位：M   
            /// <summary>
            /// 构造函数
            /// </summary>
            public ComputerExtension()
            {
                CpuId = GetCpuID();
                MacAddress = GetMacAddress();
                DiskId = GetDiskID();
                IpAddress = GetIPAddress();
                LoginUserName = GetUserName();
                SystemType = GetSystemType();
                TotalPhysicalMemory = GetTotalPhysicalMemory();
                ComputerName = GetComputerName();
            }
            public static ComputerExtension Instance()
            {
                return _instance ?? (_instance = new ComputerExtension());
            }
            /// <summary>
            /// 获取cpu序列号
            /// </summary>
            /// <returns></returns>
            private string GetCpuID()
            {
                try
                {
                    //获取CPU序列号代码   
                    string cpuInfo = ""; //cpu序列号   
                    var mc = new ManagementClass("Win32_Processor");
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                    }
                    return cpuInfo;
                }
                catch
                {
                    return "unknow";
                }
            }
            /// <summary>
            /// 获取网卡硬件地址  
            /// </summary>
            /// <returns></returns>
            private string GetMacAddress()
            {
                try
                {
                    string mac = "";
                    var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        if ((bool)mo["IPEnabled"])
                        {
                            mac = mo["MacAddress"].ToString();

                            break;
                        }
                    }
                    return mac;
                }
                catch
                {
                    return "unknow";
                }
            }
            /// <summary>
            /// 获取IP地址
            /// </summary>
            /// <returns></returns>
            private string GetIPAddress()
            {
                try
                {
                    string st = "";
                    var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        if ((bool)mo["IPEnabled"])
                        {
                            var ar = (Array)(mo.Properties["IpAddress"].Value);
                            st = ar.GetValue(0).ToString();
                            break;
                        }
                    }
                    return st;
                }
                catch
                {
                    return "unknow";
                }
            }
            /// <summary>
            /// 获取硬盘ID 
            /// </summary>
            /// <returns></returns>
            private string GetDiskID()
            {
                try
                {
                    String hDid = "";
                    var mc = new ManagementClass("Win32_DiskDrive");
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        hDid = (string)mo.Properties["Model"].Value;
                    }
                    return hDid;
                }
                catch
                {
                    return "unknow";
                }
            }

            /// <summary>   
            /// 操作系统的登录用户名   
            /// </summary>   
            /// <returns></returns>   
            private string GetUserName()
            {
                try
                {
                    string st = "";
                    var mc = new ManagementClass("Win32_ComputerSystem");
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        st = mo["UserName"].ToString();
                    }
                    return st;
                }
                catch
                {
                    return "unknow";
                }
            }
            /// <summary>   
            /// PC类型   
            /// </summary>   
            /// <returns></returns>   
            private string GetSystemType()
            {
                try
                {
                    string st = "";
                    var mc = new ManagementClass("Win32_ComputerSystem");
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        st = mo["SystemType"].ToString();
                    }
                    return st;
                }
                catch
                {
                    return "unknow";
                }
            }
            /// <summary>   
            /// 物理内存   
            /// </summary>   
            /// <returns></returns>   
            private string GetTotalPhysicalMemory()
            {
                try
                {
                    string st = "";
                    var mc = new ManagementClass("Win32_ComputerSystem");
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        st = mo["TotalPhysicalMemory"].ToString();
                    }
                    return st;
                }
                catch
                {
                    return "unknow";
                }
            }
            /// <summary>   
            ///  电脑名称
            /// </summary>   
            /// <returns></returns>   
            private string GetComputerName()
            {
                try
                {
                    return Environment.GetEnvironmentVariable("ComputerName");
                }
                catch
                {
                    return "unknow";
                }
            }
        }
        #endregion
    }
